package com.example.cxz2021606127.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.cxz2021606127.R;
import com.example.cxz2021606127.adapter.ViewPagerAdapter;
import com.example.cxz2021606127.fragment.ShouCangFragment;
import com.example.cxz2021606127.fragment.HomePageFragment;
import com.example.cxz2021606127.fragment.PersonFragment;
import com.example.cxz2021606127.util.ExitActivityUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //    private Toolbar toolbar;
    private ViewPager2 vpMainViewpager2;
    private LinearLayout shouye;
    private ImageView ivShouye;
    private TextView tvShouye;
    private LinearLayout dangjian;
    private ImageView ivDangjian;
    private TextView tvDangjian;
    private LinearLayout geren;
    private ImageView ivGeren;
    private TextView tvGeren;
    private List<Fragment> fragmentList = new ArrayList<>();

    //第一次点击退出发生的时间
    private long firstExitTime;

    //点击两次返回键退出app   System.currentTimeMillis()表示系统当前时间
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - firstExitTime > 2000)) {   //系统当前时间减去第一次点击退出时间大于2秒
                Toast toast = Toast.makeText(getApplicationContext(), "再按一次退出最新闻", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);  //改变Toast的位置
                toast.show();
                firstExitTime = System.currentTimeMillis();
            } else {
                ExitActivityUtil.getInstance().exitAllActivity();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //添加该Activity到ExitActivityUtil对象实例容器中
        ExitActivityUtil.getInstance().addActivity(this);

        initView();

        //添加各个页面的fragment
        fragmentList.add(new HomePageFragment());
        fragmentList.add(new ShouCangFragment());
        fragmentList.add(new PersonFragment());

        ivShouye.setSelected(true);
        tvShouye.setEnabled(true);
        vpMainViewpager2.setCurrentItem(0);


        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList);
        vpMainViewpager2.setAdapter(adapter);


        //viewpager2滑动监听器
        vpMainViewpager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                change(position);

                vpMainViewpager2.setUserInputEnabled(false);


            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });


    }

    private void change(int position) {
        switch (position) {
            case R.id.shouye:
                vpMainViewpager2.setCurrentItem(0);
            case 0:
                ivShouye.setSelected(true);
                ivDangjian.setSelected(false);
                ivGeren.setSelected(false);

                tvShouye.setEnabled(true);
                tvDangjian.setEnabled(false);
                tvGeren.setEnabled(false);

                break;


            case R.id.dangjian:
                vpMainViewpager2.setCurrentItem(1);
            case 1:
                ivShouye.setSelected(false);
                ivDangjian.setSelected(true);
                ivGeren.setSelected(false);

                tvShouye.setEnabled(false);
                tvDangjian.setEnabled(true);
                tvGeren.setEnabled(false);
                break;


            case R.id.geren:
                vpMainViewpager2.setCurrentItem(2);
            case 2:
                ivShouye.setSelected(false);
                ivDangjian.setSelected(false);
                ivGeren.setSelected(true);

                tvShouye.setEnabled(false);
                tvDangjian.setEnabled(false);
                tvGeren.setEnabled(true);
                break;
        }
    }

    private void initView() {

        vpMainViewpager2 = (ViewPager2) findViewById(R.id.vp_main_viewpager2);
        shouye = (LinearLayout) findViewById(R.id.shouye);
        ivShouye = (ImageView) findViewById(R.id.iv_shouye);
        tvShouye = (TextView) findViewById(R.id.tv_shouye);
        dangjian = (LinearLayout) findViewById(R.id.dangjian);
        ivDangjian = (ImageView) findViewById(R.id.iv_dangjian);
        tvDangjian = (TextView) findViewById(R.id.tv_dangjian);
        geren = (LinearLayout) findViewById(R.id.geren);
        ivGeren = (ImageView) findViewById(R.id.iv_geren);
        tvGeren = (TextView) findViewById(R.id.tv_geren);
        shouye.setOnClickListener(this);
        dangjian.setOnClickListener(this);
        geren.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        change(v.getId());

    }
}